﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Collections.Specialized;

namespace BPMS.Utility.Web
{
    public class URLUtility
    {
        /// <summary>
        /// 获取站点根目录URI,结尾始终包含“/”符号,如"http://www.siteroot.com/"
        /// </summary>
        public static string RootUrl
        {
            get
            {
                string appPath = "";
                HttpContext HttpCurrent = HttpContext.Current;
                HttpRequest request;
                if (HttpCurrent != null)
                {
                    request = HttpCurrent.Request;

                    string urlAuthority = request.Url.GetLeftPart(UriPartial.Authority);
                    if (request.ApplicationPath == null || request.ApplicationPath == "/")
                        appPath = urlAuthority;//Web站点
                    else
                        appPath = urlAuthority + request.ApplicationPath;//虚拟子目录
                }
                string s = StringHelper.LastBacklash(appPath);
                return s;
            }
        }

        /// <summary>
        /// 取得网站根目录的物理路径,结尾始终包含“\”符号
        /// </summary>
        /// <returns></returns>
        public static string RootPath
        {
            get
            {
                string AppPath = "";
                HttpContext HttpCurrent = HttpContext.Current;
                if (HttpCurrent != null)
                {
                    AppPath = HttpCurrent.Server.MapPath("~");
                }
                else
                {
                    AppPath = AppDomain.CurrentDomain.BaseDirectory;
                    if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)
                        AppPath = AppPath.Substring(0, AppPath.Length - 1);
                }
                if (AppPath.Substring(AppPath.Length - 1, 1) != "\\")
                    AppPath += "\\";

                return AppPath;
            }
        }

        /// <summary>将已经为 HTTP 传输进行过 HTML 编码的字符串转换为已解码的字符串</summary>
        public static string HtmlDecode(string s)
        {
            return HttpUtility.HtmlDecode(s);
        }

        /// <summary>将字符串转换为 HTML 编码的字符串</summary>
        public static string HtmlEncode(string s)
        {
            return HttpUtility.HtmlEncode(s);
        }

        /// <summary>对 URL 字符串进行编码</summary>
        public static string UrlEncode(string s)
        {
            return HttpUtility.UrlEncode(s);
        }

        /// <summary>将已经为在 URL 中传输而编码的字符串转换为解码的字符串</summary>
        public static string UrlDecode(string s)
        {
            return HttpUtility.UrlDecode(s);
        }
        /// <summary>
        /// 获取当前请求的来源URL
        /// </summary>
        /// <returns></returns>
        public static string UrlReferrer()
        {
            Uri uri = HttpContext.Current.Request.UrlReferrer;
            if (null != uri)
                return uri.ToString();
            return string.Empty;
        }
        /// <summary>
        /// 获取此次请求的URL
        /// </summary>
        /// <returns></returns>
        public static string RequestUrl()
        {
            Uri uri = HttpContext.Current.Request.Url;
            if (uri != null)
                return uri.ToString();
            return string.Empty;
        }

        /// <summary>
        /// 将符合URL QueryString格式的字符串转化为NameValueCollection
        /// </summary>
        /// <param name="queryString">URL QueryString格式的字符串</param>
        /// <param name="encoding">编码</param>
        /// <returns>NameValueCollection,queryString为空或者转化失败时返回NULL</returns>
        public static NameValueCollection ConvertNameValueString(string queryString, Encoding encoding)
        {
            if (null == queryString || "" == queryString)
            {
                return null;
            }
            if (null == encoding)
            {
                return System.Web.HttpUtility.ParseQueryString(queryString);
            }
            return System.Web.HttpUtility.ParseQueryString(queryString, encoding);
        }

        /// <summary>
        /// 将符合URL QueryString格式的字符串转化为NameValueCollection,默认使用UTF8编码
        /// </summary>
        /// <param name="queryString">URL QueryString格式的字符串</param>
        /// <returns>NameValueCollection,queryString为空或者转化失败时返回NULL</returns>
        public static NameValueCollection ConvertNameValueString(string queryString)
        {
            return ConvertNameValueString(queryString, null);
        }
    }
}
